import React from 'react';
import PieChart from '../../investmentAnalysis/charts/PieChart';

export default ({ data }) => {
  const symbolMap = {};
  data.forEach((item) => {
    if (symbolMap[item.symbol] === undefined) {
      symbolMap[item.symbol] = item.price * item.quantity;
    } else {
      symbolMap[item.symbol] += item.price * item.quantity;
    }
  });

  const pieData = [];
  for (const name in symbolMap) {
    pieData.push({ value: symbolMap[name].toFixed(2), name });
  }

  pieData.sort((a, b) => (a.value - b.value));


  if (pieData !== undefined && pieData.length > 0) {
    let min = pieData[0].value;
    let max = pieData[pieData.length - 1].value;
    max *= 1.6;
    min *= 0.9;

    return (
      <dl>
        <dt style={{ textAlign: 'center' }}>持仓资产分布</dt>
        <dd><PieChart title="" max={max} min={min} data={pieData} /></dd>
      </dl>
    );
  } else {
    return (
      <dl>
        <dt style={{ textAlign: 'center' }}>持仓资产分布</dt>
      </dl>
    );
  }
};
